// This file is a part of ChaosDB.
// Copyright (C) 2012 ChaoJianPeng
// email:chaojianpeng@gmail.com
//
// ChaosDB is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// ChaosDB is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

/*
 * lru.cpp
 *
 *  Created on: 2012-3-4
 *      Author: chao
 */

#include "lru.h"
#include "conf.h"
#include "cmalloc.h"
#include "hashmap.h"
#include "logger.h"

LruList* lruCreat() {
	LruList* l;
	l = (LruList*) cmalloc(sizeof(LruList));
	l->head = l->tail = NULL;
	l->len = 0;
	return l;
}
/**
 * 仅用于新增
 */
void lruAdd(LruList* l, Entry* entry) {
	if (l->len == 0) {
		l->head = l->tail = entry;
		entry->lruLeft = entry->lruRight = NULL;
	} else {
		entry->lruLeft = NULL;
		entry->lruRight = l->head;
		l->head->lruLeft = entry;
		l->head = entry;
	}
	l->len++;
}

void lruToHead(LruList* l, Entry* entry) {
	// 长度为0
	if (l->len == 0) {
		lruAdd(l, entry);
		return;
	}
	// 已经在队头
	if (l->head == entry)
		return;

	// 缝合
	if (entry->lruLeft != NULL && entry->lruRight != NULL) {
		entry->lruLeft->lruRight = entry->lruRight;
		entry->lruRight->lruLeft = entry->lruLeft;
		l->len--;
	}
	// 特殊考虑队尾
	else if (entry->lruLeft != NULL) {
		entry->lruLeft->lruRight = NULL;
		l->tail = entry->lruLeft;
		l->len--;
	}

	lruAdd(l, entry);
}

/**
 * 只是从lru中删去
 */
void lruRemove(LruList* l, Entry* entry) {
	// 判断是否在lru
	if (entry->lruLeft == NULL && entry->lruRight == NULL) {
		return;
	}
	// 缝合
	if (entry->lruLeft != NULL && entry->lruRight != NULL) {
		entry->lruLeft->lruRight = entry->lruRight;
		entry->lruRight->lruLeft = entry->lruLeft;
		l->len--;
	}
	// 特殊考虑队尾
	else if (entry->lruLeft != NULL) {
		entry->lruLeft->lruRight = NULL;
		l->tail = entry->lruLeft;
		l->len--;
	}

	else if (entry->lruRight != NULL) {
		entry->lruRight->lruLeft = NULL;
		l->head = entry->lruRight;
		l->len--;
	}
//	entryDel(entry);
//	cfree(entry);
}

void lruDoLru(LruList* l) {
	systemLogger("do lru");
	Entry* entry = l->tail;
	Entry* tmp;
	while (entry != NULL && getUsed() > MAX_RAM_USED) {
		tmp = entry->lruLeft;
		lruRemove(l, entry);
		if (entry->value != NULL) {
			entryRelease(entry);
		}
		entry = tmp;
	}
	if (getUsed() > MAX_RAM_USED) {
		ramFull = true;
		systemLogger("RAM is full.");
	}

	// test src
//	while (entry != NULL && l->len > 5) {
//		tmp = entry->lruLeft;
//		lruRemove(l, entry);
//		if (entry->value != NULL) {
//			entryRelease(entry);
//		}
//		entry = tmp;
//	}
}

void lruCheckLru(LruList* l) {
	if (getUsed() >= MAX_RAM_USED) {
		lruDoLru(l);
	}
	// test src
//	if (l->len == 5) {
//		lruDoLru(l);
//	}
}
